#include "WriteScores.h"
#include "TextDrawer.h"

using namespace std;

WriteScores::WriteScores(){
	//just initialize nothing

}

void WriteScores::draw(int currentScore)
{
	char chars;
	string combo;
	vector <string> comboList;
	bool newHighScore = false;

	string fileName = "scores.txt";
	ifstream fin(fileName.c_str());

	while (fin >> combo){
		comboList.push_back(combo.c_str());
	}

	fin.close();

	for (int i = 0; i < comboList.size(); i++)
	{

		TextDrawer text;

		string HighScoreTemp = comboList[i];
		int highScore = atoi(HighScoreTemp.c_str()); // 0 if text

		string currentScoreString;
		stringstream currentScoreTemp;
		currentScoreTemp << currentScore;
		currentScoreString = currentScoreTemp.str();
		
		if (currentScore >= highScore && highScore > 0 && newHighScore == false)
		{
			//string movingHighNameOne = comboList[i-1];
			string movingHighScoreOne = comboList[i];

			//comboList[i-1] = "ABC";
			comboList[i] = currentScoreString;
			newHighScore = true;

			if (i == 0)
			{
				//string movingHighNameTwo = comboList[i + 1];
				string movingHighScoreTwo = comboList[i + 1];
				//comboList[i + 1] = movingHighNameOne;
				comboList[i + 1] = movingHighScoreOne;
				//comboList[i + 3] = movingHighNameTwo;
				comboList[i + 2] = movingHighScoreTwo;
				currentScore = 0;
			}
			else if (i == 1)
			{
				//comboList[i + 1] = movingHighNameOne;
				comboList[i + 1] = movingHighScoreOne;
				currentScore = 0;
			}


			//write to file
			ofstream fout;
			fout.open(fileName.c_str());

			for (int i = 0; i < comboList.size(); i++)
			{
				fout << comboList[i] << endl;
			}

			fout.close();
		}
			//text.drawHighScoreNames(currentScoreString, -150, 50 + (i*10), -699);
	}

	
}